﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Step4.Unit2.Controllers
{
    public class RequestController : Controller
    {
        public IActionResult Add()
        {
            return View();
        }


        /// <summary>
        /// Http请求案例
        /// 如何访问当前请求的HttpRequest对象
        /// 如何获取请求的Headers\Cookies\path\method\Query\QueryString\protocal\isHttps等信息
        /// </summary>
        /// <returns></returns>
        public IActionResult Submit() 
        {
            //  HttpRequest对象的Query获取Get请求的查询参数集合
            var username = Request.Query["username"].ToString();
            var studentNo = Request.Query["studentNo"].ToString();
            var age = Convert.ToDateTime(Request.Query["age"]);

            // 请求头
            var headers = Request.Headers;
            var isHttps = Request.IsHttps;
            var method = Request.Method.ToString();
            var path = Request.Path.ToString();
            var pathBase = Request.PathBase.ToString();
            var protocal = Request.Protocol.ToString();// https 是Http2.0   http是1.0
            foreach (var heead in headers)
            {
                Console.WriteLine(heead.ToString());
            }
            Console.WriteLine(headers["User-Agent"].ToString());
            var cookies = Request.Cookies;

            return Content("添加成功");
        }


        /// <summary>
        /// 响应案例
        /// HttpResponse对象的使用
        /// </summary>
        /// <returns></returns>
        public IActionResult ResponseTest()
        {
            Response.StatusCode = (int)HttpStatusCode.Unauthorized; // HttpStatuCode是代表Http状态码的枚举类型
            return NotFound();
        }


        /// <summary>
        /// 主动返回404状态码
        /// 未找到资源
        /// </summary>
        /// <returns></returns>
        public IActionResult RetunNotFound404()
        {
            
            return NotFound();// 通过控制器分装好的方法返回状态码

            /*
                也可以通过Controller对象的Reponse属性的值(HttpResponse对象)的StatuCode属性来设置状态码
                    Response.StatusCode = (int)HttpStatusCode.NotFound;
                    return EmptyResult(); 
            */
        }


        /// <summary>
        /// 返回状态码401
        /// 身份验证未通过
        /// </summary>
        /// <returns></returns>
        public IActionResult ReturnUnauthorized401()
        {
            return Unauthorized(); // 通过控制器封装好的方法返回状态码
            /*
                也可以通过Controller的Reponse属性的值(HttpResponse对象)的StatuCode来设置状态码，然后返回给空ActionResult对象:

                    Response.StatusCode = Convert.ToInt32(HttpStatusCode.Unauthorized);
                    return EmptyResult();
            */
        }

        /// <summary>
        /// 禁止访问403
        /// </summary>
        /// <returns></returns>
        public IActionResult ReturnForbid403()
        {
            return Forbid();
        }


        /// <summary>
        /// 返回文件
        /// 可作下载之用
        /// </summary>
        /// <returns></returns>
        public IActionResult DownLoadFile()
        {
            using var ms = new MemoryStream();
            ms.Write(Encoding.Default.GetBytes("欢迎来到任我行码农场"));
            return File(ms.ToArray(), "application/oct-stream", "测试.txt");
        }
        
    }
}
